package com.xbongbong.paas.service.impl.base;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.dto.SearchDataInConditionDTO;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.feign.WorkOrderV2ExplainFeignClient;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.MultiUnitItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.ItemDataPoJo;
import com.xbongbong.paas.pojo.ProductFieldPojo;
import com.xbongbong.paas.pojo.dto.FormGetDTO;
import com.xbongbong.paas.pojo.vo.SearchDataInConditionVO;
import com.xbongbong.paas.pojo.vo.WorkOrderV2ComboVO;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.service.DepartmentService;
import com.xbongbong.paas.service.DistinctDataService;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.stage.StageHandle;
import com.xbongbong.paas.service.stage.StageHandleFactory;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.domain.entity.LabelEntity;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.ConditionExplainPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ProductSerialBalanceEntity;
import com.xbongbong.saas.enums.PaymentSourceTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.StockFilterTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.OrderEnum;
import com.xbongbong.saas.enums.business.OtherExpenseEnum;
import com.xbongbong.saas.enums.business.OtherIncomeEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentSheetDistributorEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import com.xbongbong.saas.help.SaasFormHelp;
import com.xbongbong.saas.help.SaasListHelp;
import com.xbongbong.saas.model.BatchFlowBillModel;
import com.xbongbong.saas.model.DataDictionaryModel;
import com.xbongbong.saas.model.LabelModel;
import com.xbongbong.saas.model.ProductSerialBalanceModel;
import com.xbongbong.saas.model.ProductStockModel;
import com.xbongbong.saas.service.ScoreRangeService;
import com.xbongbong.saas.util.FundUtil;
import com.xbongbong.sys.model.UserModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/8/13 15:41
 * @since v1.0
 */
@Component
public class BaseFormDataService {

    private static final Logger LOG = LoggerFactory.getLogger(BaseFormDataService.class);
    
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private SaasFormHelp saasFormHelp;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private DistinctDataService distinctDataService;
    @Resource
    private SaasListHelp saasListHelp;
    @Resource
    private StageHandleFactory stageHandleFactory;
    @Resource
    private DataDictionaryModel dataDictionaryModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private BatchFlowBillModel batchFlowBillModel;
    @Resource
    private ProductStockModel productStockModel;
    @Resource
    private ProductSerialBalanceModel productSerialBalanceModel;
    @Resource
    private LabelModel labelModel;
    @Resource
    private ScoreRangeService scoreRangeService;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private UserModel userModel;
    @Resource
    private WorkOrderV2ExplainFeignClient workOrderV2ComboFeignClient;


    public List<ItemDataPoJo> getItemList(List<ItemPoJo> itemList, Object value, List<Object> valueList, FieldAttrEntity fieldAttrEntity) {
        List<ItemDataPoJo> items = new ArrayList<>();
        if (CollectionsUtil.isEmpty(itemList)) {
            return items;
        }
        boolean isOther = false;
        boolean containsText = true;
        for (ItemPoJo item : itemList) {
            if (!ShowTypeEnum.listShow(item.getShowType())){
                continue;
            }
            ItemDataPoJo itemDataPoJo = new ItemDataPoJo(item.getText(), item.getValue(), item.getIsOther());
            if (Objects.equals(1, item.getIsOther())) {
                isOther = true;
            }
            if (CollectionsUtil.isNotEmpty(valueList)) {
                boolean isTrue = valueList.contains(item.getValue());
                // 模糊查询
                if (isTrue) {
                    if (Objects.equals(item.getText(), value) && Objects.equals(1, item.getIsOther())) {
                        // 下拉框选项具有其他值
                        itemDataPoJo.setValue(itemDataPoJo.getText());
                    }
                    items.add(itemDataPoJo);
                    containsText = false;
                } else if (Objects.equals(1, item.getIsOther()) && !ItemUtil.isDeleteCheck(valueList.get(0), fieldAttrEntity)) {
                    itemDataPoJo = new ItemDataPoJo(valueList.get(0), valueList.get(0), item.getIsOther());
                    items.add(itemDataPoJo);
                }
            } else if (!Objects.equals(1, item.getIsOther())){
                items.add(itemDataPoJo);
            }
        }
        if (isOther && containsText && Objects.nonNull(value) && StringUtil.isNotEmpty(value.toString()) && !ItemUtil.isUUID(value.toString())) {
            ItemDataPoJo itemDataPoJo = new ItemDataPoJo(value, value);
            items.add(itemDataPoJo);
        }
        return items;
    }

    public List<ItemDataPoJo> getItemList(List<ItemPoJo> itemList, Object value) {
        List<ItemDataPoJo> items = new ArrayList<>();
        if (CollectionsUtil.isEmpty(itemList)) {
            return items;
        }
        boolean isOther = false;
        boolean containsText = true;
        for (ItemPoJo item : itemList) {
            if (!ShowTypeEnum.listShow(item.getShowType())){
                continue;
            }
            ItemDataPoJo itemDataPoJo = new ItemDataPoJo(item.getText(), item.getValue(), item.getIsOther());
            if (Objects.equals(1, item.getIsOther())) {
                isOther = true;
            }
            if (value != null && StringUtil.isNotEmpty(value.toString()) ) {
                boolean isTrue = Objects.equals(item.getValue(), value);
                // 模糊查询
                Object conditionValue = value;
                if (StringUtils.isNotBlank(conditionValue.toString()) && (item.getText().contains(conditionValue.toString()) || isTrue)) {
                    if (item.getText().equals(conditionValue) && Objects.equals(1, item.getIsOther())) {
                        // 下拉框选项具有其他值
                        itemDataPoJo.setValue(itemDataPoJo.getText());
                    }
                    items.add(itemDataPoJo);
                    containsText = false;
                }
            } else {
                items.add(itemDataPoJo);
            }
        }
        if (isOther && containsText && Objects.nonNull(value) && StringUtil.isNotEmpty(value.toString()) && !ItemUtil.isUUID(value.toString())) {
            ItemDataPoJo itemDataPoJo = new ItemDataPoJo(value, value);
            items.add(itemDataPoJo);
        }
        return items;
    }
    
    public ConditionExplainPojo getFieldAttr(SearchDataInConditionDTO searchDataInConditionDTO) throws XbbException {
        // 判断是否为子表单，如果是子表单的话，attr为subForm_1.text_2
        if (searchDataInConditionDTO.getAttr().contains(StringConstant.POINT)) {
            String attr = searchDataInConditionDTO.getAttr();
            String[] attrs = attr.split("\\.");
            int two = 2;
            if (attrs.length == two) {
                searchDataInConditionDTO.setAttr(attrs[0]);
                searchDataInConditionDTO.setSubAttr(attrs[1]);
            }
        }
        String attr = searchDataInConditionDTO.getAttr();
        String subAttr = searchDataInConditionDTO.getSubAttr();
        Integer fieldType = searchDataInConditionDTO.getFieldType();
        Long formId =searchDataInConditionDTO.getFormId();
        Integer businessType =searchDataInConditionDTO.getBusinessType();
        String corpid = searchDataInConditionDTO.getCorpid();
        String originalAttr = searchDataInConditionDTO.getOriginalAttr();

        PaasFormExplainEntity explainEntity = proFormHelp.getExplainEntity(corpid, formId, businessType);
        Map<String, FieldAttrEntity> fieldMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<FieldAttrEntity> fieldList = JSONArray.parseArray(explainEntity.getExplains(), FieldAttrEntity.class);
        // 工单特殊下拉处理
        if (Objects.equals(businessType,XbbRefTypeEnum.WORK_ORDER_V2.getCode())){
            FormGetDTO formGetDTO = new FormGetDTO();
            formGetDTO.setFormId(formId);
            formGetDTO.setCorpid(corpid);
            formGetDTO.setAppId(-1L);
            BeanUtil.copyProperties(searchDataInConditionDTO,formGetDTO);
            // 当前阶段
            // 来源工单池  查询所有工单池
            XbbResponse<WorkOrderV2ComboVO> xbbResponse = workOrderV2ComboFeignClient.getComboItem(formGetDTO, LocaleContextHolder.getLocale().toString());
            if (!xbbResponse.isSuccess()) {
                throw new XbbException(xbbResponse.getCode(), xbbResponse.getMsg());
            }
            WorkOrderV2ComboVO result = xbbResponse.getResult();
            List<ItemPoJo> currentPhaseItems = result.getCurrentPhaseItems();
            List<ItemPoJo> sourcePoolItems = result.getSourcePoolItems();
            for (FieldAttrEntity fieldAttrEntity : fieldList) {
                if (Objects.equals("currentPhase",fieldAttrEntity.getSaasAttr())){
                    fieldAttrEntity.setItems(currentPhaseItems);
                }else if (Objects.equals("sourceCatalogue",fieldAttrEntity.getSaasAttr())){
                    fieldAttrEntity.setItems(sourcePoolItems);
                }
            }
        }
        for (FieldAttrEntity entity : fieldList) {
            fieldMap.put(entity.getAttr(), entity);
        }
        FieldAttrEntity fieldAttrEntity = fieldMap.get(StringUtil.isNotEmpty(originalAttr) ? originalAttr : attr);
        List<Integer> blockList = Arrays.asList(FieldTypeEnum.CREATOR_DEPT.getType(), FieldTypeEnum.BATCH.getType(), FieldTypeEnum.CREATORID.getType());
        if ((fieldAttrEntity == null && !blockList.contains(fieldType)) && !Objects.equals(searchDataInConditionDTO.getSubBusinessType(), XbbRefTypeEnum.SMART_REPLENISHMENT.getCode()) && !Objects.equals(searchDataInConditionDTO.getSubBusinessType(), XbbRefTypeEnum.WAIT_PURCHASE.getCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
        }
        FieldAttrEntity subField = null;
        boolean isSubForm = false;
        if (StringUtil.isNotEmpty(subAttr)) {
            if (!Objects.equals(fieldType, FieldTypeEnum.ADDRESS.getType())) {
                if (fieldAttrEntity == null) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
                }
                Map<String, FieldAttrEntity> subExplainMap = ExplainUtil.getExplainMapByList(fieldAttrEntity.getSubForm().getItems());
                subField = subExplainMap.get(subAttr);
                if (subField == null && !Objects.equals(subField.getFieldType(), FieldTypeEnum.CREATOR_DEPT.getType())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
                }
                isSubForm = true;
            }
        }
        ConditionExplainPojo conditionExplainPojo = new ConditionExplainPojo(fieldAttrEntity, subField, fieldMap, isSubForm, explainEntity.getDistributorMark());
        return conditionExplainPojo;
    }

    /**
     * 获取公共的条件数据列表
     * @param fieldAttrEntity 当前字段
     * @param searchDataInConditionDTO
     * @return
     * @throws XbbException
     */
    public SearchDataInConditionVO getConditionDataList(ConditionExplainPojo conditionExplainPojo, FieldAttrEntity fieldAttrEntity, SearchDataInConditionDTO searchDataInConditionDTO) throws XbbException {
        Integer distributorMark = searchDataInConditionDTO.getDistributorMark();
        String corpid = searchDataInConditionDTO.getCorpid();
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(searchDataInConditionDTO.getFieldType());
        SearchDataInConditionVO searchDataInConditionVO = new SearchDataInConditionVO();
        switch (fieldTypeEnum) {
            case USER:
            case USER_GROUP:
            case CREATORID:
            case OWNERID:
            case COUSERID:
                searchDataInConditionVO = distinctDataService.listOfUser(searchDataInConditionDTO);
                break;
            case DEPT:
            case DEPT_GROUP:
            case DEPARTMENTID:
            case CREATOR_DEPT:
                searchDataInConditionVO = distinctDataService.listOfDepartment(searchDataInConditionDTO);
                break;
            case WORK_ORDER_STAGE:
                searchDataInConditionVO = distinctDataService.listOfWorkOrderStage(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case WORK_ORDER_STATUS:
                searchDataInConditionVO = distinctDataService.listOfWorkOrderStatus(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case WORK_ORDER_TIME_OUT:
                searchDataInConditionVO = distinctDataService.listOfWorkOrderOut(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case STAR:
                searchDataInConditionVO = distinctDataService.listOfStar(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case WORK_ORDER_NODE_STATUS:
                searchDataInConditionVO = distinctDataService.listOfWorkOrderNodeStatus(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case TEXT:
            case TEXT_CHECK:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfText(conditionExplainPojo, fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case LINK_BUSINESS_SINGLE:
            case LINK_BUSINESS_MULTI:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfLinkBusiness(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case SELECT_PRODUCT:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfSelectProduct(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case SELECT_PURCHASE:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfSelectPurchase(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case SELECT_CONTRACT:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfSelectContract(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case SERIALNO:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfSelectSerialNo(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case ADDRESS:
                //查询限制
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfAddress(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case BIRTHDAY:
                //查询限制
//                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfBirthday();
                break;
            case LINK_DATA:
            case LINK_DATA_MULTI:
                //查询限制
                searchDataInConditionVO = distinctDataService.listOfLinkData(fieldAttrEntity, searchDataInConditionDTO);
                break;
            case REF_ID:
                //查询限制
                StockFilterTypeEnum stockFilterTypeEnum = StockFilterTypeEnum.getByAttr(searchDataInConditionDTO.getAttr());
                searchDataInConditionDTO.setBusinessType(stockFilterTypeEnum.getBusinessType());
                searchDataInConditionDTO.setAttr(stockFilterTypeEnum.getLinkAttr());
                searchDataInConditionDTO.setFieldType(stockFilterTypeEnum.getLinkFieldType());
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfRefId(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case BUSINESS_ID:
                //查询限制
                stockFilterTypeEnum = StockFilterTypeEnum.getByAttr(searchDataInConditionDTO.getAttr());
                searchDataInConditionDTO.setBusinessType(stockFilterTypeEnum.getBusinessType());
                searchDataInConditionDTO.setAttr(stockFilterTypeEnum.getLinkAttr());
                searchDataInConditionDTO.setFieldType(stockFilterTypeEnum.getLinkFieldType());
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                searchDataInConditionVO = distinctDataService.listOfBusinessId(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case SWITCH:
                //目前只用于产品
                searchDataInConditionVO = distinctDataService.listOfProductEnable(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case BATCH:
                List<ItemDataPoJo> list = new ArrayList<>();
                List<String> latestDistinctBatch = new ArrayList<>();
                if (Objects.equals(searchDataInConditionDTO.getSubBusinessType(), XbbRefTypeEnum.BATCH_FLOW_BILL.getCode())) {
                    latestDistinctBatch = batchFlowBillModel.getLatestDistinctBatch(searchDataInConditionDTO.getCorpid(), StringUtil.isEmpty(Objects.toString(searchDataInConditionDTO.getValue(), "")) ? null : searchDataInConditionDTO.getValue().toString());
                } else if (Objects.equals(searchDataInConditionDTO.getSubBusinessType(), XbbRefTypeEnum.STOCK_SEARCH.getCode()) || Objects.equals(searchDataInConditionDTO.getSubBusinessType(),XbbRefTypeEnum.GUARANTEE_SEARCH.getCode())) {
                    latestDistinctBatch = productStockModel.getLatestDistinctBatch(searchDataInConditionDTO.getCorpid(), StringUtil.isEmpty(Objects.toString(searchDataInConditionDTO.getValue(), "")) ? null : searchDataInConditionDTO.getValue().toString());
                }
                latestDistinctBatch.forEach(batch->list.add(new ItemDataPoJo(batch, batch)));
                searchDataInConditionVO.setItems(list);
                break;
                //逻辑下拉
            case COMBO_NUM:
                List<ItemPoJo> itemPoJoList = proFormHelp.getComboItems(fieldAttrEntity, searchDataInConditionDTO.getBusinessType());
                List<ItemDataPoJo> itemDataPoJoList = new ArrayList<>();
                itemPoJoList.forEach(itemPoJo -> {
                    ItemDataPoJo itemDataPoJo = new ItemDataPoJo();
                    BeanUtil.copyProperties(itemPoJo,itemDataPoJo);
                    itemDataPoJoList.add(itemDataPoJo);
                });
                searchDataInConditionVO.setItems(itemDataPoJoList);
                break;
            case SEQ:
                Map<String, Object> serialParam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                serialParam.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
                serialParam.put(ParameterConstant.CORPID, searchDataInConditionDTO.getCorpid());
                serialParam.put(ParameterConstant.COLUMNS, "seq");
                serialParam.put("pageSize", 10);
                serialParam.put("seq", searchDataInConditionDTO.getValue());
                List<ProductSerialBalanceEntity> entitys = productSerialBalanceModel.findEntitys(serialParam);
                List<ItemDataPoJo> DistinctSeq = new ArrayList<>();
                List<String> seqList = new ArrayList<>();
                for (ProductSerialBalanceEntity serialBalanceEntity : entitys) {
                    if (seqList.contains(serialBalanceEntity.getSeq())) {
                        continue;
                    }
                    seqList.add(serialBalanceEntity.getSeq());
                    ItemDataPoJo itemDataPoJo = new ItemDataPoJo();
                    itemDataPoJo.setText(serialBalanceEntity.getSeq());
                    itemDataPoJo.setValue(serialBalanceEntity.getSeq());
                    DistinctSeq.add(itemDataPoJo);
                }
                searchDataInConditionVO.setItems(DistinctSeq);
                break;
            case SOURCE:
                PaymentSourceTypeEnum paymentSourceTypeEnum = PaymentSourceTypeEnum.getByAttr(searchDataInConditionDTO.getAttr());
                //只查出表单的id即可
                PaasFormEntityExt searchPaasFormEntityExt = paasFormModel.getByBusinessType4Distributor(paymentSourceTypeEnum.getBusinessType(), searchDataInConditionDTO.getCorpid(), distributorMark, BasicConstant.ID);
                searchDataInConditionDTO.setBusinessType(paymentSourceTypeEnum.getBusinessType());
                searchDataInConditionDTO.setAttr(paymentSourceTypeEnum.getLinkAttr());
                searchDataInConditionDTO.setFieldType(paymentSourceTypeEnum.getLinkFieldType());
                saasListHelp.packageSearchDataCondition(searchDataInConditionDTO);
                List<ConditionsEntityExt> conditionsList = searchDataInConditionDTO.getConditionsList();
                if (Objects.nonNull(searchPaasFormEntityExt) && CollectionUtils.isNotEmpty(conditionsList)) {
                    //增加formId限制（数据查询只能通过formId限制来区分经销商和非经销商，否则会查出所有数据）
                    searchDataInConditionDTO.setFormId(searchPaasFormEntityExt.getId());
                    ConditionsEntityExt formIdCondition = new ConditionsEntityExt();
                    formIdCondition.setAttr(StringConstant.FORM_ID);
                    formIdCondition.setFieldType(FieldTypeEnum.DATAID.getType());
                    formIdCondition.setSymbol(ConditionEnum.EQUAL.getSymbol());
                    formIdCondition.setValue(Collections.singletonList((searchPaasFormEntityExt.getId())));
                    conditionsList.add(formIdCondition);
                    searchDataInConditionDTO.setConditionsList(conditionsList);
                }
                searchDataInConditionVO = distinctDataService.listOfRefId(fieldAttrEntity, searchDataInConditionDTO, fieldTypeEnum);
                break;
            case LABEL:
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("corpid", searchDataInConditionDTO.getCorpid());
                param.put("del", DelEnum.NORMAL.getDel());
                param.put("corpid", searchDataInConditionDTO.getCorpid());
                param.put("formId", searchDataInConditionDTO.getFormId());
                if (Objects.nonNull(searchDataInConditionDTO.getValue()) && StringUtil.isNotEmpty(searchDataInConditionDTO.getValue().toString())) {
                    param.put("nameLike", searchDataInConditionDTO.getValue().toString());
                }
                param.put("columns", " id, lable_name ");
                param.put("pageSize", 10);
                param.put("orderByStr ", " sort desc ");
                List<LabelEntity> labelEntities = labelModel.findEntitys(param);
                List<ItemDataPoJo> labelConditions = new ArrayList<>();
                for (LabelEntity labelEntity : labelEntities) {
                    ItemDataPoJo itemDataPoJo = new ItemDataPoJo();
                    itemDataPoJo.setText(labelEntity.getName());
                    itemDataPoJo.setValue(labelEntity.getId());
                    labelConditions.add(itemDataPoJo);
                }
                searchDataInConditionVO.setItems(labelConditions);
                break;
            default:
                break;
        }
        return searchDataInConditionVO;
    }

    /**
     * 获取下拉框的自定义的值
     * @param searchDataInConditionDTO
     * @param fieldAttrEntity
     * @param subAttr
     * @param conditionExplainPojo 增加该传参，用于获取上游已经通过formId读取到的解释信息，如distributorMark
     * @return
     * @throws XbbException
     */
    public List<ItemDataPoJo> getCustomList(SearchDataInConditionDTO searchDataInConditionDTO, FieldAttrEntity fieldAttrEntity, Boolean subAttr, ConditionExplainPojo conditionExplainPojo) throws XbbException {
        List<ItemDataPoJo> items;
        try {
            List<ItemPoJo> itemList;
            String corpid = searchDataInConditionDTO.getCorpid();
            Integer businessType = searchDataInConditionDTO.getBusinessType();
            //订货单 订单状态筛选固定值 特殊处理
            Integer distributorMark = (conditionExplainPojo == null || Objects.isNull(conditionExplainPojo.getDistributorMark())) ? searchDataInConditionDTO.getDistributorMark() : conditionExplainPojo.getDistributorMark();
            String attr = fieldAttrEntity.getAttr();
            boolean distributorMarkFlag = Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(),distributorMark);
            //经销商类型下拉字段读取
            boolean useItems = distributorMarkFlag && (
                    (Objects.equals(businessType, XbbRefTypeEnum.CONTRACT.getCode()) && Objects.equals(OrderEnum.STATUS.getAttr(), attr))
                            || (Objects.equals(businessType, XbbRefTypeEnum.PAYMENT_SHEET.getCode()) && Objects.equals(PaymentSheetDistributorEnum.PAY_METHOD.getAttr(), attr))
            );
            if(useItems){
                List<ItemPoJo> itemPoJoList = fieldAttrEntity.getItems();
                List<ItemDataPoJo> itemDataPoJoList = new ArrayList<>();
                for (ItemPoJo item : itemPoJoList) {
                    ItemDataPoJo itemDataPoJo = new ItemDataPoJo();
                    itemDataPoJo.setText(item.getText());
                    itemDataPoJo.setValue(item.getValue());
                    itemDataPoJoList.add(itemDataPoJo);
                }
                return itemDataPoJoList;
            }
            Boolean isExpress = (Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(), businessType) && Objects.equals(ContractEnum.EXPRESS_NAME.getAttr(), fieldAttrEntity.getAttr()))
                    || (Objects.equals(OutstockEnum.EXPRESS_NAME.getAttr(), fieldAttrEntity.getAttr()) && (Objects.equals(XbbRefTypeEnum.OUTSTOCK.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode(),businessType) || Objects.equals(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.OTHER_OUTSTOCK.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK.getCode(), businessType)));
            if (!subAttr) {
                //资金账户对应saasAttr
                List<String> fundAccountSaasAttrList = FundUtil.getFundAccountSaasAttrList();
                if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), CustomerManagementEnum.CUSTOMER_STAGE.getSaasAttr()))) {
                    StageHandle stageHandle = stageHandleFactory.getStageHandle(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
                    itemList = stageHandle.getItemList(corpid,searchDataInConditionDTO.getFormId(), BasicConstant.ONE);
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), SalesOpportunityEnum.SALE_STAGE.getSaasAttr()))) {
                    StageHandle stageHandle = stageHandleFactory.getStageHandle(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
                    itemList = stageHandle.getItemList(corpid,searchDataInConditionDTO.getFormId(), BasicConstant.ONE);
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), CustomerManagementEnum.TYPE.getSaasAttr()))) {
                    itemList = dataDictionaryModel.getDictionaryList4Saas(DictionaryEnum.CUSTOMER_STATUS, corpid, searchDataInConditionDTO.getFormId());
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.CONTRACT.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), ContractEnum.STATUS.getSaasAttr()))) {
                    itemList = dataDictionaryModel.getDictionaryList4Saas(DictionaryEnum.CONTRACT_STATUS, corpid, searchDataInConditionDTO.getFormId());
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.PAYMENT_SHEET.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), PaymentSheetEnum.PAY_METHOD.getSaasAttr()))) {
                    PaasFormExplainEntity paymentSheetExplainEntity = paasFormExplainModel.getByBusinessType4Distributor(XbbRefTypeEnum.PAYMENT_SHEET.getCode(), corpid, distributorMark);
                    Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(paymentSheetExplainEntity.getExplains(), PaymentSheetEnum.PAY_METHOD.getAttr());
                    itemList = explainMap.get(PaymentSheetEnum.PAY_METHOD.getAttr()).getItems();
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.PAY_SHEET.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), PaySheetEnum.PAY_TYPE.getSaasAttr()))) {
                    PaasFormExplainEntity paySheetExplainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.PAY_SHEET.getCode(), corpid);
                    Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(paySheetExplainEntity.getExplains(), PaySheetEnum.PAY_TYPE.getAttr());
                    itemList = explainMap.get(PaySheetEnum.PAY_TYPE.getAttr()).getItems();
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.OTHER_INCOME.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), OtherIncomeEnum.PAY_METHOD.getSaasAttr()))) {
                    PaasFormExplainEntity explainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.OTHER_INCOME.getCode(), corpid);
                    Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(explainEntity.getExplains(), OtherIncomeEnum.PAY_METHOD.getAttr());
                    itemList = explainMap.get(OtherIncomeEnum.PAY_METHOD.getAttr()).getItems();
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.OTHER_EXPENSE.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), OtherExpenseEnum.PAY_METHOD.getSaasAttr()))) {
                    PaasFormExplainEntity explainEntity = paasFormExplainModel.getByBusinessType(XbbRefTypeEnum.OTHER_EXPENSE.getCode(), corpid);
                    Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(explainEntity.getExplains(), OtherExpenseEnum.PAY_METHOD.getAttr());
                    itemList = explainMap.get(OtherExpenseEnum.PAY_METHOD.getAttr()).getItems();
                } else if (isExpress) {
                    // 物流公司
                    itemList = dataDictionaryModel.getDictionaryList4Saas(DictionaryEnum.EXPRESS_NAME, corpid, searchDataInConditionDTO.getFormId());
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.PAYMENT_SHEET.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), PaymentSheetEnum.PAYMENT_SHEET_TYPE.getSaasAttr()))) {
                    //判断预收款菜单是否开启
                    boolean prepaymentFlag = paasMenuService.enableMenu(corpid, XbbRefTypeEnum.PREPAYMENT_BALANCE.getAlias());
                    //根据预收款是否开启，设置预收款相关类型的显隐
                    itemList = dataDictionaryModel.getDictionaryList4PaymentSheetType(prepaymentFlag);
                } else if ((Objects.equals(searchDataInConditionDTO.getBusinessType(), XbbRefTypeEnum.PAY_SHEET.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), PaySheetEnum.PAY_SHEET_TYPE.getSaasAttr()))) {
                    //判断预付款菜单是否开启
                    boolean prepayFlag = paasMenuService.enableMenu(corpid, XbbRefTypeEnum.PREPAY_BALANCE.getAlias());
                    itemList = dataDictionaryModel.getDictionaryList4PaySheetType(prepayFlag);
                } else if (fundAccountSaasAttrList.contains(fieldAttrEntity.getSaasAttr())) {
                    itemList = proFormHelp.handleFundAccountItems(corpid, businessType);
                } else if (Objects.equals(businessType, XbbRefTypeEnum.PRODUCT.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), ProductEnum.UNIT.getSaasAttr())) {
                    ProductFieldPojo productFieldPojo = saasFormHelp.getProductFieldPojo(RedisPrefixConstant.PRODUCT_UNIT, ProductEnum.UNIT.getAttr(), corpid);
                    itemList = productFieldPojo.getSingleItems();
                    List<MultiUnitItemPoJo> multiItems = productFieldPojo.getMultiItems();
                    BeanUtil.copyPropertiesList(multiItems, itemList, ItemPoJo.class);
                }else if((Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) && Objects.equals(fieldAttrEntity.getAttr(), CustomerManagementEnum.SCORE.getAttr())) ||
                        (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode()) && Objects.equals(fieldAttrEntity.getAttr(), ClueEnum.SCORE.getAttr()))){
                    itemList = scoreRangeService.getItems4ScoreRangeScreen(corpid,searchDataInConditionDTO.getFormId());
                } else {
                    itemList = fieldAttrEntity.getItems();
                }
            }else {
                itemList = fieldAttrEntity.getItems();
            }
            // 获取符合条件的值
            if (Objects.isNull(searchDataInConditionDTO.getFrom())) {
                items = getItemList(itemList, searchDataInConditionDTO.getValue());
            } else {
                items = getItemList(itemList, searchDataInConditionDTO.getValue(), searchDataInConditionDTO.getValueList(), fieldAttrEntity);
            }
        }catch (Exception e) {
            LOG.error("paasFormData.searchDataInProcessCondition流程流转条件根据字段检索数据出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return items;
    }


}
